<!-- 填写维修 -->
<template>
  <div class="content">
    <avue-form
      class="avueForm"
      v-if="visible"
      v-model="formData"
      :option="
        autoForm === ''
          ? {}
          : { ...JSON.parse(autoForm), detail: isDetail === 1 }
      "
      @submit="requestPutTask"
    />
  </div>
</template>

<script>
import {
  // getDefinition,
  getAvueForm,
  getRecord,
  putAvueForm
} from '@/api/index'
import { Toast } from 'vant'

// window.getParams = this.getParams
export default {
  data () {
    return {
      visible: false,
      formData: {},
      config: {},
      // 当前任务的数据
      currentType: '',
      currentprocessId: '',
      currenttaskId: '',
      currentid: '',
      autoForm: '',
      isDetail: false,
      lastRoute: '',
      currentprocessIdcopy: ''
    }
  },
  created () {
    // console.log('进入 h5')
    // const { formData, config } = this.$route.query
    // this.formData = formData
    // this.config = JSON.parse(config)
  },
  mounted () {
    window.getParams = this.getParams
    window.getScanData = this.getScanData
    // 接收参数
    const res = this.$route.query.params
    const arr = res.split(',')
    this.currentType = arr[0]
    this.currentprocessId = arr[1] // intanceid
    this.currenttaskId = arr[2] // definationkey
    this.currentid = arr[3]
    this.isDetail = parseInt(arr[4])
    this.lastRoute = arr[5]
    this.currentprocessIdcopy = arr[6]
    console.log(arr, this.isDetail)
    // 查询自定义表单
    this.getDefinition()
  },
  components: {},

  computed: {},

  methods: {
    // flutter 传递来的参数---->查询对应的表单
    getParams (params) {
      console.log(params)
      const arr = params.split(',')
      this.currentType = arr[0]
      this.currentprocessId = arr[1]
      this.currenttaskId = arr[2]
      this.currentid = arr[3]
      this.isDetail = parseInt(arr[4])
      this.lastRoute = arr[5]
      this.currentprocessIdcopy = arr[6]
      // 查询自定义表单
      if (this.currentType === 'patrol') {
        Toast('巡更稍后上线')
      } else {
        this.getDefinition()
      }
      return params
    },
    // flutter 传递来的参数--->来自扫码
    async getScanData (data) {
      console.log(data)
      alert(data)
    },
    // 根据当前任务类型查询流程定义----自定义表单
    async getDefinition () {
      const data2 = {
        processId: this.currentprocessIdcopy,
        // taskId: 'StartEvent', // 报修
        taskId: this.currenttaskId,
        tenantId: window.localStorage.getItem('tenantId'),
        type: 'pc'
        // detail: true,
        // 是否查询记录
      }
      const form = await getAvueForm(data2) // 表单
      // console.log(form)
      const { detail } = form
      this.autoForm = detail
      this.visible = true
      if (Array.isArray(form) && form.length === 0) {
        alert('字段为空')
      }
      // 查询表单内容
      this.getFormDetail()
    },
    // 根据任务id查询详细信息
    async getFormDetail () {
      try {
        const res = await getRecord({
          id: this.currentprocessId,
          tenantId: window.localStorage.getItem('tenantId')
        })
        console.log('453245444444444444444', res)
        this.formData = res.processVariables
      } catch (error) {}
    },
    // 点击提交修改流程定义
    async requestPutTask (val) {
      console.log(val)
      const data = {
        variables: val,
        // 当 complete 为0时只保存数据不修改状态
        complete: 1,
        taskId: this.currentid
      }
      await putAvueForm(data)
      // 判断上级路由是 h5 或者 flutter，决定是返回上级页面或者flutter
      Toast('提交成功')
      if (this.lastRoute === 'h5') {
        this.$router.go(-1)
      } else {
        // 调用是否允许返回的方法
        // 调用flutter 的返回上级页面
        setTimeout(() => {
          this.sendData('_back')
        }, 200)
      }
    },
    // 返回flutter
    sendData (params) {
      // eslint-disable-next-line no-undef
      Flutter.postMessage(params)
    }
  }
}
</script>
<style lang='less' scoped>
.content {
  width: 100%;
  height: 100%;
  background: #fff;
  > .avueForm {
    margin-top: 15px;
  }
}
</style>
